import {StatePagingType} from "../index";
import {SecurityMenu} from "type/entity/SecurityMenu";
import RequestStatus from "../RequestStatus";
import UserStorage from "storage/local/UserStorage";
import {createSlice} from "@reduxjs/toolkit";
import {fetchAddMenu, fetchMenuPageData, fetchUpdateMenu} from "../async-action/menuAsyncAction";
import {builderPagingHandler} from "./index";

export type MenuState = StatePagingType<SecurityMenu>;

const initialState: MenuState = {
    data: [],
    loading: false,
    requestStatus: RequestStatus.INIT,
    currentPage: 1,
    pageSize: UserStorage.getInstance().getAdminPageConfigFromLocalStore().menuPageSize,
    total: 0,
    totalPage: 10
};

export const menuSlice = createSlice({
    name: "menu",
    initialState,
    reducers: {

    },
    extraReducers: (builder) => {
        builderPagingHandler(builder, fetchMenuPageData);

        builder.addCase(fetchUpdateMenu.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });
        builder.addCase(fetchAddMenu.fulfilled, (state, action) => {
            state.requestStatus = RequestStatus.REFRESH;
        });

    }
});

export const {} = menuSlice.actions;
export default menuSlice.reducer;